<template>
  <div class="subordinate-attendance-container">
    <el-card class="page-card" shadow="hover">
      <template #header>
        <div class="card-header">
          <div class="header-left">
            <el-icon class="header-icon"><View /></el-icon>
            <span class="header-title">下属考勤查看</span>
          </div>
          <div class="header-right">
            <el-button type="success" @click="exportData" :loading="exportLoading">
              <el-icon><Download /></el-icon>
              导出Excel
            </el-button>
          </div>
        </div>
      </template>
      
      <el-table 
        :data="attendanceList" 
        style="width: 100%"
        stripe
        :empty-text="'暂无考勤记录'"
      >
        <el-table-column label="员工" width="150" align="center">
          <template #default="scope">
            <span>{{ scope.row.userName || `员工${scope.row.userId}` }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="type" label="类型" width="120" align="center">
          <template #default="scope">
            <el-tag v-if="scope.row.type === 'CHECK_IN'" type="success" effect="dark">
              <el-icon><CircleCheck /></el-icon>
              上班
            </el-tag>
            <el-tag v-else-if="scope.row.type === 'CHECK_OUT'" type="info" effect="dark">
              <el-icon><CircleClose /></el-icon>
              下班
            </el-tag>
            <el-tag v-else type="warning" effect="dark">
              <el-icon><Timer /></el-icon>
              加班
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="checkInTime" label="打卡时间" width="180" align="center">
          <template #default="scope">
            <span class="time-text">{{ formatDate(scope.row.checkInTime || scope.row.checkOutTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="100" align="center">
          <template #default="scope">
            <el-tag v-if="scope.row.status === 'APPROVED'" type="success">已通过</el-tag>
            <el-tag v-else-if="scope.row.status === 'PENDING'" type="warning">待审核</el-tag>
            <el-tag v-else type="danger">已拒绝</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="180" align="center">
          <template #default="scope">
            <span class="time-text">{{ formatDate(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import request from '../utils/request'
import { ElMessage } from 'element-plus'
import { View, CircleCheck, CircleClose, Timer, Download } from '@element-plus/icons-vue'

export default {
  name: 'SubordinateAttendance',
  components: {
    View,
    CircleCheck,
    CircleClose,
    Timer,
    Download
  },
  setup() {
    const attendanceList = ref([])
    const exportLoading = ref(false)
    
    const formatDate = (dateStr) => {
      if (!dateStr) return ''
      return new Date(dateStr).toLocaleString('zh-CN')
    }
    
    const loadAttendanceList = async () => {
      try {
        const res = await request.get('/attendance/subordinate')
        attendanceList.value = res.data || []
      } catch (error) {
        ElMessage.error('加载考勤记录失败')
      }
    }
    
    // 导出数据
    const exportData = async () => {
      exportLoading.value = true
      try {
        const token = localStorage.getItem('token')
        const url = '/api/attendance/subordinate/export'
        
        const response = await fetch(url, {
          method: 'GET',
          headers: {
            'Authorization': `Bearer ${token}`
          }
        })
        
        if (!response.ok) {
          throw new Error('导出失败')
        }
        
        const blob = await response.blob()
        const downloadUrl = window.URL.createObjectURL(blob)
        const link = document.createElement('a')
        link.href = downloadUrl
        link.download = `团队考勤记录_${new Date().toISOString().slice(0, 10)}.xlsx`
        document.body.appendChild(link)
        link.click()
        document.body.removeChild(link)
        window.URL.revokeObjectURL(downloadUrl)
        
        ElMessage.success('导出成功')
      } catch (error) {
        ElMessage.error('导出失败：' + (error.message || '未知错误'))
      } finally {
        exportLoading.value = false
      }
    }
    
    onMounted(() => {
      loadAttendanceList()
    })
    
    return {
      attendanceList,
      formatDate,
      exportLoading,
      exportData
    }
  }
}
</script>

<style scoped>
.subordinate-attendance-container {
  width: 100%;
  height: 100%;
}

.page-card {
  border-radius: 12px;
  border: none;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 12px;
}

.header-icon {
  font-size: 24px;
  color: #409eff;
}

.header-title {
  font-size: 20px;
  font-weight: 600;
  color: #1a1a1a;
}

.time-text {
  color: #666;
  font-size: 14px;
}

:deep(.el-table) {
  border-radius: 8px;
  overflow: hidden;
}

:deep(.el-table th) {
  background-color: #fafafa;
  color: #333;
  font-weight: 600;
}

:deep(.el-table td) {
  padding: 16px 0;
}
</style>

